/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package edu.ncsu.csc.sensors;

/**
 *
 * @author Richard Pospesel
 */
public class GroWeatherThermometer implements Sensor
{
	public GroWeatherThermometer()
	{
		UARTInterface.initialize();
	}

	public char getValue()
	{
		byte[] carriage = {(byte)0x0d};
		byte[] cmd = {(byte)'W', (byte)'R', (byte)'D', (byte)0x34, (byte)0x36, (byte)0x0d};
		byte[] response;
		synchronized(UARTInterface.class)
		{
			try
			{
				byte ack = 0;
				do
				{
					UARTInterface.send_byte_array(carriage);
					UARTInterface.receive_byte_array(1, 1500);
					UARTInterface.send_byte_array(cmd);
					ack = UARTInterface.receive_byte_array(1, 1500)[0];
				}
				while(ack != 0x06);
				response = UARTInterface.receive_byte_array(2, 1500);
			}
			catch(Exception e)
			{
				response = new byte[2];
			}
		}
		char result = (char)(0xFF & response[0]);
		result += (char)(256 * (0xFF & response[1]));
		//System.out.println("Temperature: " + result/10.0);
		// 0 to 120 degrees
		result = (char)(result * 54.6125);
		return result;
	}
}
